package com.xqx.controller;

import com.xqx.domain.ResultInfo;
import com.xqx.domain.User;
import com.xqx.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * @author：xingquanxiang createTime：2019/10/5 13:17
 * description:
 */
@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/list")
    public ModelAndView userList(){
        ModelAndView modelAndView = new ModelAndView();
        List<User> userList = userService.findAll();
        modelAndView.addObject("userList", userList);
        modelAndView.setViewName("user-list");
        return modelAndView;
    }

    @RequestMapping("/del")
    @ResponseBody
    public ResultInfo del(int id){
        ResultInfo resultInfo = new ResultInfo();
        if (userService.del(id)){
            resultInfo.setFlag(true);
            resultInfo.setData(userService.findAll());
            resultInfo.setMessage("删除成功!");
        }else {
            resultInfo.setFlag(false);
            resultInfo.setMessage("删除失败");
        }
        return resultInfo;
    }
}
